$(function () {
    var sendUrl = '/access/sendCode?mail=';
    var registerUrl = '/access/register';

    //验证邮箱格式
    function checkEmail(email){
        var exg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if(email.match(exg)){
            return true;
        }else{
            return false;
        }
    }

    //点击发送验证码到指定邮箱
    $('#sendCode').click(function () {
        var mail = $('#email').val();
        if (mail == '') {
            $('#warn').text('您未填写邮箱');
        }else if (!checkEmail(mail)) {
            $('#warn').text('邮箱格式不正确');
        }else {
            var url = sendUrl + mail;
            $.getJSON(url,function (data) {
                alert(data.message);
            })
        }
    })

    //提交注册信息
    $('#submit').click(function () {
        var mail = $('#email').val();
        var password = $('#password').val();
        var code = $('#code').val();
        if (mail == ''||password==''||code==''||!checkEmail(mail)){
            //先判断输入的内容是否完善
            alert('填写信息不完善或有误');
        } else {
            var url = registerUrl + '?mail='+mail+'&&password='+password+'&&code=' + code;
            $.getJSON(url,function (data) {
                alert(data.message);
                if (data.code == 200) {
                    //注册成功则经过1秒跳转到登录页面
                    countDown(1,'/frontend/login');
                }
            })
        }
    })

});